package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> saveCategory(@RequestBody Category category, HttpServletRequest request) {

        log.info("新增分类,分类信息为:{}", category.toString());

        //检查表,找到需要进行处理的数据

        //公共字段已经自动填充

        //4.将新分类信息存入数据库
        boolean save = categoryService.save(category);

        if (save) {
            return R.success("新增成功");
        } else {
            return R.error("保存失败");
        }
    }

    /**
     * 分类信息分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R getByPageAndCondition(Integer page, Integer pageSize) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize);

        //分页构造器
        Page pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加排序条件
        lqw.orderByAsc(Category::getSort);
        //执行查询
        categoryService.page(pageInfo, lqw);

        List records = pageInfo.getRecords();
        int total = (int) pageInfo.getTotal();

        //只返回前端需要的数据
        Map data = new HashMap();
        data.put("records", records);
        data.put("total", total);

        return R.success(data);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R delCategory(Long id) {
        /*LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, id);
        if (dishService.count(lqw) > 0) {
            //throw new CustomException("该分类下有菜品,不能删除");
            return R.error("该分类下有菜品,不能删除");
        }

        LambdaQueryWrapper<Setmeal> lqwMeal = new LambdaQueryWrapper<>();
        lqwMeal.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(lqwMeal) > 0) {
            //throw new CustomException("该分类下有套餐,不能删除");
            return R.error("该分类下有套餐,不能删除");
        }*/

        categoryService.remove(id);

        return R.success("删除成功");
    }

    /**
     * 根据id修改分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateEmp(@RequestBody Category category) {
        log.info(category.toString());

        categoryService.updateById(category);

        return R.success("修改成功");
    }

    /**
     * 根据条件查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R getList(Integer type){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null , Category::getType,type)
                .orderByDesc(Category::getSort);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
